package com.javaking.clanteam.studentutils;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class MainActivity extends SherlockFragmentActivity {

    @SuppressLint("NewApi")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final View activityRootView = findViewById(R.id.activityRoot);
		activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
		  @Override
		  public void onGlobalLayout() {
		    int keyboardHidden = getResources().getConfiguration().keyboardHidden;
			if (keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) {
		    	AdView adView = (AdView) findViewById(R.id.ad);
		    	adView.stopLoading();
	        	adView.setVisibility(View.GONE);
		    } else if (keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
		    	checkForAds(MainActivity.this);
		    }
		  }
		});
		
		if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) {
			ActionBar bar = getActionBar();
			if (bar!=null) {
				bar.setTitle(R.string.app_name);
			}
		}
		
    }
    
    /* (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onResume()
     */
    @Override
    protected void onResume() {
    	super.onResume();
    	
    	checkForAds(this);
    }
    
    /* (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)
     */
    @Override
    public void onAttachFragment(Fragment fragment) {
    	super.onAttachFragment(fragment);
    	
    	checkForAds(this);
    }
    
    /* (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onBackPressed()
     */
    @Override
    public void onBackPressed() {
    	super.onBackPressed();
    	
    	checkForAds(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(R.string.menu_settings).setIcon(android.R.drawable.ic_menu_preferences)
    		.setIntent(new Intent(this,SettingsActivity.class));
    	getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    /**
	 * If ads are enabled, this will tell the ad to load. If not, it will make the
	 * adView disappear
	 */
	public static void checkForAds(Activity context) {
		StackTraceElement[] stackTrace = new Exception().getStackTrace();
		StackTraceElement element = stackTrace[1];
		System.out.println(element.toString());
		Log.i("Ads", "Checking for to see if ads are enabled");
		AdView adView = (AdView)context.findViewById(R.id.ad);
		if (adView==null)
		{
			return;
		}
        if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
        		context.getResources().getString(R.string.enableAds), true)) {
        	Log.i("Ads", "Ads are enabled, requesting a new ad.");
        	AdRequest adRequest = new AdRequest();
    		String[] testDevices = context.getResources().getStringArray(R.array.testDevices);
    		for(String testDevice: testDevices) {
    			adRequest.addTestDevice(testDevice);
    		}
    		adRequest.addKeyword("marching band");
    		adView.loadAd(adRequest);
    		adView.setVisibility(View.VISIBLE);
        } else {
        	Log.i("Ads", "Ads are disabled, hiding adView.");
        	adView.stopLoading();
        	adView.setVisibility(View.GONE);
        }
	}
}
